package com.hww.common.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

public class BitmapBase64Utils {

    /**
     * base64照片编码 转换为 照片bitmap
     *
     * @param string
     * @return
     */
    public static Bitmap stringtoBitmap(String string) {
        //将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            /*bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);*/
            /*bitmap = ImageUtils.getImageFromData(bitmapArray, 480, 640);*/

            int mMaxWidth = 480;
            int mMaxHeight = 640;
            BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
            if (mMaxWidth == 0 && mMaxHeight == 0) {
                decodeOptions.inPreferredConfig = Bitmap.Config.RGB_565;
                bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length,
                        decodeOptions);
            } else {
                // If we have to resize this image, first get the natural bounds.
                decodeOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length, decodeOptions);
                int actualWidth = decodeOptions.outWidth;
                int actualHeight = decodeOptions.outHeight;
                // Then compute the dimensions we would ideally like to decode to.
                int desiredWidth = ImageUtils.getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight);
                int desiredHeight = ImageUtils.getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth);
                decodeOptions.inJustDecodeBounds = false;
                decodeOptions.inSampleSize = ImageUtils.findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
                Bitmap tempBitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length, decodeOptions);
                if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
                    bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
                    tempBitmap.recycle();
                } else {
                    bitmap = tempBitmap;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    /**
     * 照片bitmap转换为 String格式base64编码
     *
     * @param bitmap
     * @return
     */
    public static String bitmaptoString(Bitmap bitmap) {
        //将Bitmap转换成字符串
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        string = Base64.encodeToString(bytes, Base64.DEFAULT);
        return string;
    }

}
